package com.cx.base.view.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.cx.base.R;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Author by ziv, Email wuming5445@dingtalk.com, Date on 2020/9/29.
 */
public abstract class BaseFragment extends IFragment {
    protected ViewGroup mRootView;
    private Unbinder unbinder;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.AppDialog);
        initParams(getArguments());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        initDialog();
        mRootView = (ViewGroup) initByViewBind(inflater, container);
        if (mRootView == null)
            mRootView = (ViewGroup) inflater.inflate(initLayoutRes(), container, false);
        unbinder = ButterKnife.bind(this, mRootView);
        initViewAndData();
        bindPresenter();
        initViewEvent();
        return mRootView;
    }


    protected View initByViewBind(LayoutInflater inflater, @Nullable ViewGroup container) {
        return null;
    }


    protected void initParams(Bundle bundle) {

    }

    private void initDialog() {
        Dialog dialog = getDialog();
        if (dialog != null) {
            Window window = getDialog().getWindow();
            if (window != null)
                initWindow(window);
        }
    }

    public void initWindow(Window window) {
        window.setGravity(Gravity.CENTER);
        window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    protected abstract @LayoutRes
    int initLayoutRes();


    protected void initViewAndData() {

    }


    protected void initViewEvent() {

    }


    protected abstract void bindPresenter();


}
